<!--
 * @Author: william-occam
 * @Date: 2024-07-19 22:00:03
 * @LastEditors: william-occam
 * @LastEditTime: 2024-10-16 00:29:00
 * @FilePath: \draw-project\src\views\welcome\index.vue
 * @Description:
 *
 *  Copyright 2024 by GETECH  Co., Ltd. All Rights Reserved.
 *  Licensed under the GETECH License,  Version 1.0;
 *  https://www.getech.cn/licenses/LICENSE-1.0
-->
<!--
 * @Author: heyang
 * @Date: 2024-07-19 09:30:32
 * @LastEditors: william-occam
 * @LastEditTime: 2024-07-19 22:25:27
 * @FilePath: \draw-project\src\views\welcome\index.vue
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
-->

<script setup lang="ts">
defineOptions({
  name: "Welcome"
});
import { effect, onMounted, reactive, ref } from "vue";
import { useColumns } from "./columns";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import AddFill from "@iconify-icons/ep/plus";
import Delete from "@iconify-icons/ep/delete";
import * as XLSX from 'xlsx';
import { ipcRenderer } from "electron";

interface RowVO {
  id: number;
  name: string;
  role: string;
  sex: string;
  age: number;
  address: string;
}

const { columns, dataList, onAdd, onDel, adaptiveConfig, selectOptions, selectVal, fileList,resolvingData, zhuanJiaListStore,handleChange, exportExcel,selectChange } = useColumns();
 // 监听主进程的回复
 ipcRenderer.on('file-data', (event, data) => {
      // 处理接收到的文件数据
   console.log('Received file data:', data);
   zhuanJiaListStore.setWhiteList(data.split(',') || [])
 });

ipcRenderer.on('sendStashFilesData', (event, data) => {
  const { type, jsonData } = data;
      // 处理接收到的文件数据
  const fileData = resolvingData(jsonData)
  if (type === 'gongcheng') {
    dataList.value = fileData;
          zhuanJiaListStore.setZhuanJiaList(fileData || [])
        }
  else if (type === 'yiliao') {
    zhuanJiaListStore.setYiliaoList(fileData || [])
        } else {
          zhuanJiaListStore.setZhengFulistList(fileData || [])
        }
 });

onMounted(() => {
  const gongChengFilePath = localStorage.getItem('gongChengFilePath');
  const zhengFuFilePath = localStorage.getItem('zhengFuFilePath');
  const yiLiaoFilePath = localStorage.getItem('yiliaoFilePath');
  if (gongChengFilePath)
    ipcRenderer.send('readStashFiles', {type: 'gongcheng', filePath: gongChengFilePath })
  if(zhengFuFilePath)
  ipcRenderer.send('readStashFiles',{type: 'zhengfu', filePath: zhengFuFilePath })
  if (yiLiaoFilePath)
  ipcRenderer.send('readStashFiles',{type: 'yiliao', filePath: yiLiaoFilePath })
})



</script>

<template>
  <div class="my-2">
    <div class="flex my-2">
      <el-select v-model="selectVal" placeholder="Select" style="width: 240px" @change="selectChange">
      <el-option
        v-for="item in selectOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <!-- <el-button @click="importEvent" class="ml12">导入</el-button> -->
    <el-upload
    v-model:file-list="fileList"
    class="upload"
    action=""
    :limit="1"
    :auto-upload="false"
    :on-change="handleChange"
    :show-file-list="false"
    ref="upLoadRef"
  >
    <el-button type="primary" @click="() => {
      ($refs.upLoadRef as any).clearFiles()
    }">导入</el-button>
  </el-upload>
    <el-button @click="exportExcel" class="downLoad">下载</el-button>
    </div>
    <pure-table
      row-key="id"
      align-whole="center"
      :header-cell-style="{
        background: 'var(--el-fill-color-light)',
        color: 'var(--el-text-color-primary)',
      }"
      :data="dataList"
      :columns="columns"
      :adaptiveConfig="adaptiveConfig"
    >
      <template #empty>
        <Empty fill="var(--el-svg-monochrome-grey)" class="m-auto" />
      </template>
      <template #append>
        <div class="pr-4">
        <el-button
          plain
          class = "mr-2  ml-2 my-4 w-full"
          :icon="useRenderIcon(AddFill)"
          @click="onAdd"
        >
          添加一行数据
        </el-button>
      </div>
      </template>
      <template #operation="{ row }">
        <el-button
          class="reset-margin"
          link
          type="primary"
          :icon="useRenderIcon(Delete)"
          @click="onDel(row)"
        />
      </template>
    </pure-table>
  </div>
</template>

<style scoped>
  .ml12 {
    margin-left: 12px
  }
  .flex {
    display: flex;
    .upload {
      margin-left: 12px
    }
    .downLoad {
      margin-left: 12px
    }
  }
</style>
